//
//  Project_TabViewController.m
//  CMVRFForIOS
//
//  Created by HailongHan on 15/3/27.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "Project_TabViewController.h"
#import "ProjectList_TabViewController.h"
#import "Project_Step1ViewController.h"
#import "ProjectManager.h"
#import "UITapImageView.h"
#import "UITapView.h"
#import "SearchProjectViewController.h"

@interface Project_TabViewController ()

@property (nonatomic,strong) UISearchBar *projectSearchBar;

@property (nonatomic,strong) XTSegmentControl *mXTSegmentControl;
@property (nonatomic,strong) iCarousel *miCarousel;
@property (assign, nonatomic) NSInteger oldSelectedIndex;

@property (nonatomic,strong) NSMutableArray *mViewControllerList;

@end

@implementation Project_TabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"5C";
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(backTop)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"back_top"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(searchProject)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [self.navigationItem.rightBarButtonItem setImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    [self setupViews];
//    [self createAddBtn];
}

- (void)backTop {
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showMainViewController];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.miCarousel = ({
        iCarousel *icarousel = [[iCarousel alloc] init];
        icarousel.dataSource = self;
        icarousel.delegate = self;
        icarousel.decelerationRate = 1.0;
        icarousel.scrollSpeed = 1.0;
        icarousel.type = iCarouselTypeLinear;
        icarousel.pagingEnabled = YES;
        icarousel.clipsToBounds = YES;
        icarousel.bounceDistance = 0.2;
        [self.view addSubview:icarousel];
        
        [icarousel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(44, 0, 0, 0));
        }];
        icarousel;
    });
    
    //添加滑块
    self.mXTSegmentControl = [[XTSegmentControl alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 44) Items:@[@"进行中",@"已成交",@"未成交"] selectedBlock:^(NSInteger index) {
        if (index == self.oldSelectedIndex) {
            return;
        }
        
        self.oldSelectedIndex = index;
        [self.miCarousel scrollToItemAtIndex:index animated:NO];
    }];
    self.mXTSegmentControl.backgroundColor = UIColor16(0xeeeeee);
    [self.view addSubview:self.mXTSegmentControl];
    self.oldSelectedIndex = 0;
    [self initData];
}

/**
 发布群消息
 */
- (void)createAddBtn {
    //加号按钮
    UITapImageView *add = [[UITapImageView alloc] initWithImage:[UIImage imageNamed:@"project_new"]];
    add.frame = CGRectMake(kScreen_Width - 100, kScreen_Out_NavAndStatus_Height - 150, 48, 48);
    add.contentMode = UIViewContentModeScaleToFill;
    if (iPhone_X) {
        add.y -= 34;
    }
    
    [add addTargetBlock:^(id obj) {
        [self buildNewProject];
    }];
    
    [self.view addSubview:add];
    [self.view bringSubviewToFront:add];
}

#pragma mark - 初始化数据
- (void)initData{
    self.mViewControllerList = [NSMutableArray array];
    
    ProjectList_TabViewController *runningProjectVC = [ProjectList_TabViewController new];
//    runningProjectVC.projectSelection = ^(ProjectData * _Nonnull project) {
//        Project_Step1ViewController *view = [Project_Step1ViewController new];
//        view.project_id = project.project_id;
//        [self.navigationController pushViewController:view animated:YES];
//    };
    runningProjectVC.type = 1;
    
    ProjectList_TabViewController *notDealedProjectVC = [ProjectList_TabViewController new];
    notDealedProjectVC.type = 2;
    
    ProjectList_TabViewController *dealedProjectVC = [ProjectList_TabViewController new];
    dealedProjectVC.type = 4;
    
    [self addChildViewController:runningProjectVC];
    [self addChildViewController:notDealedProjectVC];
    [self addChildViewController:dealedProjectVC];
    
    [self.mViewControllerList addObject:runningProjectVC];
    [self.mViewControllerList addObject:notDealedProjectVC];
    [self.mViewControllerList addObject:dealedProjectVC];
}

- (void)refreshData {
    ProjectList_TabViewController *runningProjectVC = [self.mViewControllerList objectAtIndex:0];
    [runningProjectVC refreshData];
}

#pragma mark - 事件处理
- (void)buildNewProject{
    //[Tools showMessageCenter:@"你点击了新建项目"];
    [[ProjectManager manager]clearProjectInfo];
    Project_Step1ViewController *view = [Project_Step1ViewController new];
    [self.navigationController pushViewController:view animated:YES];
}

- (void)searchProject{
    //[Tools showMessageCenter:@"你点击了新建项目"];
    SearchProjectViewController *view = [SearchProjectViewController new];
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - UISearchBar代理
/**
 *  called when text changes (including clear)
 *
 *  @param searchBar  <#searchBar description#>
 *  @param searchText <#searchText description#>
 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"%@", searchBar.text);
}
/**
 *  用户编辑时的响应
 *
 *  @param searchBar 控件的ID
 */
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    NSLog(@"%@", searchBar.text);
}

/**
 *  called when keyboard search button
 *
 *  @param searchBar <#searchBar description#>
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"%@", searchBar.text);
    NSLog(@"%@", searchBar.text);
    if ([self.projectSearchBar isFirstResponder]) {
        [self.projectSearchBar resignFirstResponder];
    }
}

#pragma mark - iCarousel代理
-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    return 3;
}

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    UIViewController *vc = [self.mViewControllerList objectAtIndex:index];
    return vc.view;
}

//开始移动
-(void)carouselDidScroll:(iCarousel *)carousel{
    [self.view endEditing:YES];
    
    if (self.mXTSegmentControl) {
        CGFloat offset = self.miCarousel.scrollOffset;
        if (offset > 0) {
            [self.mXTSegmentControl moveIndexWithProgress:offset];
        }
    }
}

//移动结束
-(void)carouselDidEndScrollingAnimation:(iCarousel *)carousel{
    if (self.mXTSegmentControl) {
        [self.mXTSegmentControl endMoveIndex:carousel.currentItemIndex];
    }
    
    if (self.oldSelectedIndex != carousel.currentItemIndex) {
        self.oldSelectedIndex = carousel.currentItemIndex;
    }
}

@end
